package com.ruoyi.system.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ProvideInfo;
import com.ruoyi.system.service.impl.ProvideInfoServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProvideAllRecords;
import com.ruoyi.system.service.IProvideAllRecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商付款Controller
 * 
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/system/provideAllRecords")
public class ProvideAllRecordsController extends BaseController
{
    @Autowired
    private IProvideAllRecordsService provideAllRecordsService;
    @Autowired
    private ProvideInfoServiceImpl provideInfoService;


    /**
     * 查询供应商付款列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HttpServletResponse response,ProvideAllRecords provideAllRecords)
    {
        startPage();
        List<ProvideAllRecords> list = provideAllRecordsService.selectProvideAllRecordsList(provideAllRecords);
        Double sumMoney = 0.0;
        Double getMoney = provideAllRecordsService.selectSumMoney(provideAllRecords.getProvide(), provideAllRecords.getFinanceCode(), provideAllRecords.getCreateTime(),provideAllRecords.getStatus());
        if(getMoney!=null&&getMoney>0.0){
            sumMoney=getMoney;
        }
        DecimalFormat df = new DecimalFormat("0.00##");
        response.setHeader("sumMoney",df.format(sumMoney));
        return getDataTable(list);
    }

    /**
     * 导出供应商付款列表
     */
    @Log(title = "供应商付款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProvideAllRecords provideAllRecords)
    {
        List<ProvideAllRecords> list = provideAllRecordsService.selectProvideAllRecordsList(provideAllRecords);
        ExcelUtil<ProvideAllRecords> util = new ExcelUtil<ProvideAllRecords>(ProvideAllRecords.class);
        util.exportExcel(response, list, "供应商付款数据");
    }

    /**
     * 获取供应商付款详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(provideAllRecordsService.selectProvideAllRecordsById(id));
    }

    /**
     * 新增供应商付款
     */
    @Log(title = "供应商付款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProvideAllRecords provideAllRecords)
    {
        return toAjax(provideAllRecordsService.insertProvideAllRecords(provideAllRecords));
    }

    /**
     * 修改供应商付款
     */
    @Log(title = "供应商付款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProvideAllRecords provideAllRecords)
    {
        return toAjax(provideAllRecordsService.updateProvideAllRecords(provideAllRecords));
    }

    /**
     * 删除供应商付款
     */
    @Log(title = "供应商付款", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(provideAllRecordsService.deleteProvideAllRecordsByIds(ids));
    }

    /**
     * 落盘
     */

    @RequestMapping("/addProvideAll")
    public AjaxResult addProvideAll(@RequestParam("time")String time)
    {
        List<Map<String, Object>> maps = provideAllRecordsService.selectAllRecords(time);
        if(maps!=null&& maps.size()>0){
            DecimalFormat df = new DecimalFormat("0.00##");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String updateTime = sdf.format(new Date());
            for (Map<String,Object> map:maps) {
                 String create_time = map.get("create_time").toString();
                 String provide = map.get("provide").toString();
                 String money = map.get("money").toString();
                 String priceMoney = map.get("priceMoney").toString();
                money = df.format(Double.valueOf(money));
                 ProvideAllRecords provideAllRecords = new ProvideAllRecords();
                 provideAllRecords.setProvide(provide);
                 provideAllRecords.setCreateTime(create_time);
                 provideAllRecords.setFinanceCode("");
                 provideAllRecords.setTaxRed("0");
                 provideAllRecords.setActMoney("0");
                 provideAllRecords.setPlatformMoney(priceMoney);
                 provideAllRecords.setPayableMoney(money);
                 provideAllRecords.setUnpaidMoney(money);
                 provideAllRecords.setValuationTax("0");
                 provideAllRecords.setUpdateTime(updateTime);
                 provideAllRecords.setStatus("0");
                 provideAllRecordsService.insertProvideAllRecords(provideAllRecords);
            }
        }
        return AjaxResult.success("数据添加成功");
    }

    /**
     * 落盘
     */
    @RequestMapping("/updateFinanceCode")
    public AjaxResult updateFinanceCode()
    {
        List<ProvideAllRecords> provideAllRecords = provideAllRecordsService.selectProvideAllRecordsList(new ProvideAllRecords());
        List<ProvideInfo> provideInfos = provideInfoService.selectProvideInfoList(new ProvideInfo());
        for (ProvideAllRecords provideAllRecords1:provideAllRecords) {
            for (ProvideInfo provideInfo:provideInfos) {
                if(provideAllRecords1.getProvide().equals(provideInfo.getName())){
                    provideAllRecords1.setFinanceCode(provideInfo.getFinanceCode());
                    provideAllRecordsService.updateProvideAllRecords(provideAllRecords1);
                }
            }
        }

        return AjaxResult.success("数据添加成功");
    }

}
